/**
 * 
 */
package com.xbang.attendance.entity.order;

import java.io.Serializable;
import java.util.Date;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import com.xbang.attendance.entity.system.FareFacilitator;
import com.xbang.attendance.entity.system.MaintenanceStation;
import com.xbang.attendance.entity.system.Region;
import com.xbang.attendance.entity.system.Warehouse;
import com.xbang.attendance.security.support.SecurityUserHolder;
import com.xbang.attendance.utils.DateUtil;
import com.xbang.core.utils.DateUtils;

import enumeration.DeliveryStatus;

/**
 * @author dev.zhang
 * 
 */
@Entity
public class DeliveryOrder implements Serializable {

	private static final long serialVersionUID = 7912419842355446557L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Integer id;

	private String number;

	/** PO */
	@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY, targetEntity = PurchaseOrder.class)
	@JoinColumn(columnDefinition = "purchase_order", referencedColumnName = "id")
	private PurchaseOrder purchaseOrder;

	/** 维修站 */
	@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY, targetEntity = MaintenanceStation.class)
	@JoinColumn(columnDefinition = "maintenance_station", referencedColumnName = "id")
	private MaintenanceStation maintenanceStation;

	/** 仓储商 */
	@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY, targetEntity = Warehouse.class)
	@JoinColumn(columnDefinition = "warehouse", referencedColumnName = "id")
	private Warehouse warehouse;

	/** 推荐仓储商 */
	@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY, targetEntity = Warehouse.class)
	@JoinColumn(columnDefinition = "product_warehouse", referencedColumnName = "id")
	private Set<Warehouse> productWareHouses;

	/** 运费服务商 */
	@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY, targetEntity = FareFacilitator.class)
	@JoinColumn(columnDefinition = "fare_facilitator", referencedColumnName = "id")
	private FareFacilitator fareFacilitator;

	/** 明细数据 */
	@OneToMany(cascade = { CascadeType.REFRESH }, fetch = FetchType.LAZY, mappedBy = "deliveryOrder", targetEntity = DeliveryDetails.class)
	private Set<DeliveryDetails> deliveryDetails;

	/** 区域 */
	@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY, targetEntity = Region.class)
	@JoinColumn(columnDefinition = "region", referencedColumnName = "id")
	private Region region;
	
	/**是否手工匹配*/
	@Column(name = "manual_generic")
	@org.hibernate.annotations.Type(type = "yes_no")
	private boolean manualGeneric;

	/** DO产生日期 */
	private Date deliveryDate;

	// /已签收、结算中、已结算
	/** 送货单状态 */
	@Enumerated(EnumType.STRING)
	private DeliveryStatus deliveryStatus;
	

	/** 生成人 */
	private String generateor;

	/** 生成日期 */
	private Date generateorDate;

	/** 发送人 */
	private String sendor;

	/** 发送日期 */
	private Date sendDate;

	// ign, Settlement, Settled
	/** 签收人 */
	private String signor;

	/** 签收日期 */
	private Date signDate;

	/** 结算中操作人 */
	private String settlementor;

	/** 结算中操作日期 */
	private Date settlementDate;

	/** 已结算操作人 */
	private String settledor;

	/** 已结算操作日期 */
	private String settledDate;

	/** 是否发送邮件 */
	@JoinColumn(name = "send_mail")
	@org.hibernate.annotations.Type(type = "yes_no")
	private boolean sendEmail = false;

	public boolean isCanEditStatus() {

		return !(this.deliveryStatus.compareTo(DeliveryStatus.Generate) == 0 || this.deliveryStatus.compareTo(DeliveryStatus.Settled) == 0);

	}

	public DeliveryStatus getNextDeliveryStatus() {

		if (deliveryStatus.compareTo(DeliveryStatus.Generate) == 0) {
			return DeliveryStatus.Send;
		} else if (deliveryStatus.compareTo(DeliveryStatus.Send) == 0) {
			return DeliveryStatus.Sign;
		} else if (deliveryStatus.compareTo(DeliveryStatus.Sign) == 0) {
			return DeliveryStatus.Settlement;
		} else if (deliveryStatus.compareTo(DeliveryStatus.Settlement) == 0) {
			return DeliveryStatus.Settled;
		} else if (deliveryStatus.compareTo(DeliveryStatus.Settled) == 0) {
			return DeliveryStatus.Settled;
		}

		return this.deliveryStatus;
	}

	public String getDeliveryStatusString(DeliveryStatus deliveryStatus) {
		if (deliveryStatus.compareTo(DeliveryStatus.Generate) == 0) {
			return "Generate";
		} else if (deliveryStatus.compareTo(DeliveryStatus.Send) == 0) {
			return "Send";
		} else if (deliveryStatus.compareTo(DeliveryStatus.Sign) == 0) {
			return "Sign";
		} else if (deliveryStatus.compareTo(DeliveryStatus.Settlement) == 0) {
			return "Settlement";
		} else if (deliveryStatus.compareTo(DeliveryStatus.Settled) == 0) {
			return "Settled";
		} else {
			return null;
		}
	}

	/**
	 * 根据状态拿到应该更新的人员字段
	 * 
	 * @param deliveryStatus
	 * @return
	 */
	public static String getUpdateDeliveryStatusPersonFieldName(DeliveryStatus deliveryStatus) {

		if (deliveryStatus.compareTo(DeliveryStatus.Generate) == 0) {
			return "generateor";
		} else if (deliveryStatus.compareTo(DeliveryStatus.Send) == 0) {
			return "sendor";
		} else if (deliveryStatus.compareTo(DeliveryStatus.Sign) == 0) {
			return "signor";
		} else if (deliveryStatus.compareTo(DeliveryStatus.Settlement) == 0) {
			return "settlementor";
		} else if (deliveryStatus.compareTo(DeliveryStatus.Settled) == 0) {
			return "settledor";
		} else {
			return null;
		}

	}
	
	//public static getUpdateDeliveryStatusPersonFieldName()
	/**
	 * 根据状态拿到应该更新的日期字段
	 * 
	 * @param deliveryStatus
	 * @return
	 */
	public static String getUpdateDeliveryStatusDateFieldName(DeliveryStatus deliveryStatus) {

		if (deliveryStatus.compareTo(DeliveryStatus.Generate) == 0) {
			return "generateor_date";
		} else if (deliveryStatus.compareTo(DeliveryStatus.Send) == 0) {
			return "send_date";
		} else if (deliveryStatus.compareTo(DeliveryStatus.Sign) == 0) {
			return "sign_date";
		} else if (deliveryStatus.compareTo(DeliveryStatus.Settlement) == 0) {
			return "settlement_date";
		} else if (deliveryStatus.compareTo(DeliveryStatus.Settled) == 0) {
			return "settled_date";
		} else {
			return null;
		}

	}

	public String getDeliveryStatusDisplay() {
		if (deliveryStatus == null) {
			return "生成";
		}
		if (deliveryStatus.compareTo(DeliveryStatus.Generate) == 0) {
			return "生成";
		} else if (deliveryStatus.compareTo(DeliveryStatus.Send) == 0) {
			return "发送";
		} else if (deliveryStatus.compareTo(DeliveryStatus.Sign) == 0) {
			return "签收";
		} else if (deliveryStatus.compareTo(DeliveryStatus.Settlement) == 0) {
			return "结算中";
		} else if (deliveryStatus.compareTo(DeliveryStatus.Settled) == 0) {
			return "已结算";
		} else {
			return "生成";
		}
	}

	/**
	 * @param purchaseOrder
	 * @param maintenanceStation
	 * @param warehouse
	 * @return
	 */
	public DeliveryOrder initDelivery(PurchaseOrder purchaseOrder, MaintenanceStation maintenanceStation, Warehouse warehouse) {
		this.setDeliveryDate(new Date());
		this.setDeliveryStatus(DeliveryStatus.Generate);
		this.setMaintenanceStation(maintenanceStation);
		this.setWarehouse(warehouse);
		this.setNumber("DO".concat(maintenanceStation.getNumber()).concat(DateUtils.format(new Date(), "yyyyMMddhhmmss")));
		this.setPurchaseOrder(purchaseOrder);
		this.setRegion(purchaseOrder.getMaintenanceStation().getRegion());
		this.setGenerateor(SecurityUserHolder.getCurrentUser().getUsername());
		this.setGenerateorDate(new Date());
		return this;
	}

	/**
	 * 初始化送货单，本身的仓库，和推荐仓储商
	 * 
	 * @param purchaseOrder
	 * @param maintenanceStation
	 * @param warehouse
	 * @return
	 */
	public DeliveryOrder initDelivery(PurchaseOrder purchaseOrder, MaintenanceStation maintenanceStation, Warehouse warehouse, Set productWareHouses,
			String doNumber) {
		this.setDeliveryDate(new Date());
		this.setDeliveryStatus(DeliveryStatus.Generate);
		this.setMaintenanceStation(maintenanceStation);
		this.setWarehouse(warehouse);
		if (doNumber == null || doNumber.equals(""))
			this.setNumber("DO".concat(maintenanceStation.getNumber()).concat(DateUtils.format(new Date(), "yyyyMMddhhmmss")));
		else {
			this.setNumber(doNumber);
		}
		this.setPurchaseOrder(purchaseOrder);
		this.setRegion(purchaseOrder.getMaintenanceStation().getRegion());
		this.setGenerateor(SecurityUserHolder.getCurrentUser().getUsername());
		this.setGenerateorDate(new Date());
		this.setProductWareHouses(productWareHouses);
		return this;
	}

	@Transient
	private String className;

	/** 根据当前时间，查看邮件是否已经发送，并标明当前的颜色 */
	public String getClassName() {
		

		if (!sendEmail) {		
			if (this.deliveryDate == null)
				return "";
			int day = DateUtil.getDistDates(this.deliveryDate, new Date());

			if (day < 2) {
				return "purple";
			} else {
				return "err";
			}
		}
		if(this.deliveryStatus.equals(DeliveryStatus.Send)){
			if (this.sendDate == null)
				return "";
			int day = DateUtil.getDistDates(this.sendDate, new Date());
			if (day < 2) {
				return "orange";
			} else {
				return "err";
			}
		}
		return "";
	}

	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * @return the number
	 */
	public String getNumber() {
		return number;
	}

	/**
	 * @param number
	 *            the number to set
	 */
	public void setNumber(String number) {
		this.number = number;
	}

	/**
	 * @return the purchaseOrder
	 */
	public PurchaseOrder getPurchaseOrder() {
		return purchaseOrder;
	}

	/**
	 * @param purchaseOrder
	 *            the purchaseOrder to set
	 */
	public void setPurchaseOrder(PurchaseOrder purchaseOrder) {
		this.purchaseOrder = purchaseOrder;
	}

	/**
	 * @return the maintenanceStation
	 */
	public MaintenanceStation getMaintenanceStation() {
		return maintenanceStation;
	}

	/**
	 * @param maintenanceStation
	 *            the maintenanceStation to set
	 */
	public void setMaintenanceStation(MaintenanceStation maintenanceStation) {
		this.maintenanceStation = maintenanceStation;
	}

	/**
	 * @return the fareFacilitator
	 */
	public FareFacilitator getFareFacilitator() {
		return fareFacilitator;
	}

	/**
	 * @param fareFacilitator
	 *            the fareFacilitator to set
	 */
	public void setFareFacilitator(FareFacilitator fareFacilitator) {
		this.fareFacilitator = fareFacilitator;
	}

	/**
	 * @return the region
	 */
	public Region getRegion() {
		return region;
	}

	/**
	 * @param region
	 *            the region to set
	 */
	public void setRegion(Region region) {
		this.region = region;
	}

	/**
	 * @return the deliveryDate
	 */
	public Date getDeliveryDate() {
		return deliveryDate;
	}

	/**
	 * @param deliveryDate
	 *            the deliveryDate to set
	 */
	public void setDeliveryDate(Date deliveryDate) {
		this.deliveryDate = deliveryDate;
	}

	/**
	 * @return the deliveryStatus
	 */
	public DeliveryStatus getDeliveryStatus() {
		return deliveryStatus;
	}

	/**
	 * @param deliveryStatus
	 *            the deliveryStatus to set
	 */
	public void setDeliveryStatus(DeliveryStatus deliveryStatus) {
		this.deliveryStatus = deliveryStatus;
	}

	/**
	 * @return the generateor
	 */
	public String getGenerateor() {
		return generateor;
	}

	/**
	 * @param generateor
	 *            the generateor to set
	 */
	public void setGenerateor(String generateor) {
		this.generateor = generateor;
	}

	/**
	 * @return the generateorDate
	 */
	public Date getGenerateorDate() {
		return generateorDate;
	}

	/**
	 * @param generateorDate
	 *            the generateorDate to set
	 */
	public void setGenerateorDate(Date generateorDate) {
		this.generateorDate = generateorDate;
	}

	/**
	 * @return the sendor
	 */
	public String getSendor() {
		return sendor;
	}

	/**
	 * @param sendor
	 *            the sendor to set
	 */
	public void setSendor(String sendor) {
		this.sendor = sendor;
	}

	/**
	 * @return the sendDate
	 */
	public Date getSendDate() {
		return sendDate;
	}

	/**
	 * @param sendDate
	 *            the sendDate to set
	 */
	public void setSendDate(Date sendDate) {
		this.sendDate = sendDate;
	}

	/**
	 * @return the signor
	 */
	public String getSignor() {
		return signor;
	}

	/**
	 * @param signor
	 *            the signor to set
	 */
	public void setSignor(String signor) {
		this.signor = signor;
	}

	/**
	 * @return the signDate
	 */
	public Date getSignDate() {
		return signDate;
	}

	/**
	 * @param signDate
	 *            the signDate to set
	 */
	public void setSignDate(Date signDate) {
		this.signDate = signDate;
	}

	/**
	 * @return the settlementor
	 */
	public String getSettlementor() {
		return settlementor;
	}

	/**
	 * @param settlementor
	 *            the settlementor to set
	 */
	public void setSettlementor(String settlementor) {
		this.settlementor = settlementor;
	}

	/**
	 * @return the settlementDate
	 */
	public Date getSettlementDate() {
		return settlementDate;
	}

	/**
	 * @param settlementDate
	 *            the settlementDate to set
	 */
	public void setSettlementDate(Date settlementDate) {
		this.settlementDate = settlementDate;
	}

	/**
	 * @return the settledor
	 */
	public String getSettledor() {
		return settledor;
	}

	/**
	 * @param settledor
	 *            the settledor to set
	 */
	public void setSettledor(String settledor) {
		this.settledor = settledor;
	}

	/**
	 * @return the settledDate
	 */
	public String getSettledDate() {
		return settledDate;
	}

	/**
	 * @param settledDate
	 *            the settledDate to set
	 */
	public void setSettledDate(String settledDate) {
		this.settledDate = settledDate;
	}

	/**
	 * @return the warehouse
	 */
	public Warehouse getWarehouse() {
		return warehouse;
	}

	/**
	 * @param warehouse
	 *            the warehouse to set
	 */
	public void setWarehouse(Warehouse warehouse) {
		this.warehouse = warehouse;
	}

	public Set<Warehouse> getProductWareHouses() {
		return productWareHouses;
	}

	public void setProductWareHouses(Set<Warehouse> productWareHouses) {
		this.productWareHouses = productWareHouses;
	}

	/**
	 * @return the deliveryDetails
	 */
	public Set<DeliveryDetails> getDeliveryDetails() {
		return deliveryDetails;
	}

	/**
	 * @param deliveryDetails
	 *            the deliveryDetails to set
	 */
	public void setDeliveryDetails(Set<DeliveryDetails> deliveryDetails) {
		this.deliveryDetails = deliveryDetails;
	}

	/**
	 * @return the sendEmail
	 */
	public boolean isSendEmail() {
		return sendEmail;
	}

	/**
	 * @param sendEmail
	 *            the sendEmail to set
	 */
	public void setSendEmail(boolean sendEmail) {
		this.sendEmail = sendEmail;
	}

	public boolean isManualGeneric() {
		return manualGeneric;
	}

	public void setManualGeneric(boolean manualGeneric) {
		this.manualGeneric = manualGeneric;
	}

	
}
